const app = getApp();
const key_login_info = "LoginInfo";

class LoginModel {
    apiPath = "login/userLogin";

    /**
     * 执行登录
     * @param mobile 登录手机号
     * @param password 登录密码
     * @param onLoginSuccess 登录成功的回调，在此回调中拥有LoginInfo信息，结构为{"token":"string","ptype":"5","utype":"5"}
     * @param onComplete 完成时的回调，无论登录成功或失败
     */
    login(mobile, password, onLoginSuccess, onComplete) {
        let that = this;
        app.request(this.apiPath, {
            type: "passwdLogin",
            mobile: mobile,
            password: password
        }, function (res) {
            that.resetLoginInfo(res.data, onLoginSuccess)
        }, function (res) {
            wx.showToast({
                title: res.info,
                icon: 'none'
            })
        }, function (res) {
            onComplete()
        })
    }

    /**
     *验证码登录
      * @param mobile 手机号
     * @param code 验证码
     * @param onLoginSuccess 登录成功的回调，在此回调中拥有LoginInfo信息，结构为{"token":"string","ptype":"5","utype":"5"}
     * @param onComplete 完成时的回调，无论登录成功或失败
     */
    loginWithCode(mobile, code, onLoginSuccess, onComplete) {
        let that = this;
        app.request(this.apiPath, {
            type: "smsLogin",
            mobile: mobile,
            code: code
        }, function (res) {
            that.resetLoginInfo(res.data, onLoginSuccess)
        }, function (res) {
            wx.showToast({
                title: res.info,
                icon: 'none'
            })
        }, function (res) {
            onComplete()
        })
    }

    resetLoginInfo(loginInfo, onSuccess) {
        app.globalData.loginInfo = loginInfo;
        wx.setStorage({
            key: key_login_info,
            data: loginInfo,
            success: onSuccess ? onSuccess(loginInfo) : null
        })
    }

    /**
     * 平台登录，使用code换取session_key
     * @param code 调用微信登录后，返回的随机码
     * @param onSuccess code换取session_key成功后回调 数据:{"openid":"ee8d5664e0408b9172c7672c4dcaecca","session_key":"2343edsr23eq2fewaw21"}
     */
    loginByPlatform(onSuccess) {
        let that = this;
        wx.login({
            success: function (res) {
                if (res.code) {
                    app.request(that.apiPath, {
                        type: "wxAuthCode",
                        code: res.code
                    }, function (res) {
                        onSuccess(res)
                    }, function (res) {
                        wx.showToast({
                            title: res.info,
                            icon: 'none'
                        })
                    })
                }
            }
        })
    }

    /**
     * 获取登录信息。注：本登录信息是保存到微信上的，获取完成之后如不为空会保存到app全局数据中
     */
    getLoginInfo() {
        const loginInfo = wx.getStorageSync(key_login_info);
        if (loginInfo) {
            app.globalData.loginInfo = loginInfo;
        }
        return loginInfo;
    }

    /**
     * 清除缓存中的登录信息
     */
    clearLoginInfo() {
        wx.removeStorage({
            key: key_login_info,
            success: function (res) {
                wx.reLaunch({
                    url: '/pages/Sign/sign'
                })
            }
        })
    }
}

export {
    LoginModel
}